package liuliu.throughwaring.ui.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.support.annotation.DrawableRes;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import net.tsz.afinal.annotation.view.CodeNote;

import java.io.ByteArrayOutputStream;
import java.util.List;

import liuliu.custom.control.toolbar.TToolbar;
import liuliu.custom.method.Utils;
import liuliu.throughwaring.R;
import liuliu.throughwaring.base.BaseActivity;
import liuliu.throughwaring.control.picture.IPictureView;
import liuliu.throughwaring.control.picture.PictureListener;
import liuliu.throughwaring.model.PoliceModel;
import liuliu.throughwaring.model.TerminalModel;
import liuliu.throughwaring.view.PictureView;
import liuliu.throughwaring.view.Util;

/**
 * Created by liuliu on 2015/11/18   18:24
 * 警员详细信息
 *
 * @author 柳伟杰
 * @Email 1031066280@qq.com
 */
public class PoliceDetailActivity extends BaseActivity {
    @CodeNote(id = R.id.police_detail_remark)
    TextView police_detail_remark;//备注
    @CodeNote(id = R.id.police_detail_createtime)
    TextView police_detail_createtime;//创建时间
    @CodeNote(id = R.id.police_detail_phone)
    TextView police_detail_phone;//联系电话
    @CodeNote(id = R.id.police_detail_pid)
    TextView police_detail_pid;//编码
    @CodeNote(id = R.id.police_detail_name)
    TextView police_detail_name;//姓名
    PoliceModel policeModel;
    @CodeNote(id = R.id.police_detail_toolbar)
    TToolbar unit_detail_toolbar;
    @CodeNote(id = R.id.police_detail_image)
    ImageView policeHeader;
    @CodeNote(id = R.id.call_btn, click = "onClick")
    Button call_btn;
    Bitmap person_img;

    @Override
    public void initViews() {
        setContentView(R.layout.activity_police_detail);
        String policeId = getIntent().getStringExtra("policeId");
        List<PoliceModel> list = finalDb.findAllByWhere(PoliceModel.class, "PoliceId='" + policeId + "'");
        if (list != null) {
            if (list.size() > 0) {
                policeModel = list.get(0);
            }
        }
        policeHeader.setImageResource(R.mipmap.person_header);
    }

    @Override
    public void initEvents() {
        unit_detail_toolbar.setLeftOnClick(new TToolbar.LeftOnClickListener() {
            @Override
            public void leftclick() {
                finish();
            }
        });
        police_detail_name.setText(policeModel.getPoliceName());
        police_detail_pid.setText(policeModel.getPoliceId());
        police_detail_remark.setText(Util.checkEmpty(policeModel.getPoliceComment(), "无"));
        police_detail_createtime.setText(policeModel.getPoliceCreateTime());
        police_detail_phone.setText(policeModel.getPolicePhoneNumber());
        policeHeader.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {//点击头像放大
                Intent intent = new Intent(PoliceDetailActivity.this, PictureView.class);
                if (person_img == null) {
                    person_img = ((BitmapDrawable) policeHeader.getDrawable()).getBitmap();
                }
                String p = Utils.encodeBitmap(person_img);
                intent.putExtra("picture", p);
                startActivity(intent);
            }
        });
        boolean result = false;
        if (policeModel.getPoliceCardImage() != null) {
            if (!policeModel.getPoliceCardImage().equals("") && !policeModel.getPoliceCardImage().equals("null")) {
                result = true;
                person_img = Utils.getLoacalBitmap(policeModel.getPoliceCardImage());
                policeHeader.setImageBitmap(Utils.centerSquareScaleBitmap(person_img, 60));
            }
        }
        if (!result) {
            policeHeader.setImageResource(R.mipmap.center_picture);
        }
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.call_btn://拨打电话
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + policeModel.getPolicePhoneNumber()));
                startActivity(intent);
                break;
        }
    }
}
